// module game {

//     export class GameAdPage extends eui.Component {

//         public static readonly SHOW_FRIEND_RANK: string = "show_friend_rank";

//         public static readonly PLAY_GAME_EVENT: string = "start_game";

//         public holdBg: eui.Image;

//         public static adImgDatas: Array<AdImgData>;

//         public rootGroup: eui.Group;

//         public scroller: eui.Scroller;

//         public appIdDataGroup: eui.DataGroup;

//         private btnHoldOpen: eui.Image;

//         private btnHoldClose: eui.Image;

//         public btnGetRewardImg: eui.Image;

//         public adDataGroup: eui.Group;

//         public adGroup: eui.Group;

//         public bgImg: eui.Image;

//         private buttonHoldOpen: ui.PushButton;

//         private buttonHoldClose: ui.PushButton;

//         private buttonGetReward: ui.PushButton;

//         private static readonly APP_ID_URL: string = "https://2zhuji.cn/index.php?g=WxaGame&m=Ad&a=get_ad_list&token=";

//         constructor() {
//             super();
//             this.addEventListener(eui.UIEvent.COMPLETE, this.onComplete, this);
//             this.skinName = "resource/assets/GameRes/game/skins/GameActivityPageSkin.exml";
//         }

//         public show(): void {
//             this.visible = true;
//         }

//         protected createChildren() {
//             super.createChildren();
//         }

//         public resize(): void {

//         }

//         private onComplete(): void {
//             this.bgImg.visible = false;
//             this.bgImg.width = manager.DisplayManager.STAGE_WIDTH;
//             this.bgImg.height = manager.DisplayManager.STAGE_HEIGHT;
//             this.buttonHoldClose = new ui.PushButton(this.btnHoldClose);
//             this.buttonHoldClose.addEventListener(ui.PushButton.CLICK, this.onCloseOpen, this);
//             this.adDataGroup.addChild(this.buttonHoldClose);

//             this.buttonHoldOpen = new ui.PushButton(this.btnHoldOpen);
//             this.buttonHoldOpen.addEventListener(ui.PushButton.CLICK, this.onOpenHold, this);
//             this.adDataGroup.addChild(this.buttonHoldOpen);

//             this.buttonHoldClose.visible = false;
//             this.buttonHoldOpen.visible = true;

//             let dataGroup = this.appIdDataGroup;//对象是eui.DataGroup;
//             dataGroup.itemRenderer = AdItemRender;
//             dataGroup.useVirtualLayout = true;
//             dataGroup.scrollEnabled = true;

//             let tLayout: eui.TileLayout = new eui.TileLayout();

//             tLayout.requestedRowCount = 2;
//             tLayout.requestedColumnCount = 3;
//             tLayout.verticalGap = 15;
//             tLayout.horizontalGap = 30;
//             dataGroup.layout = tLayout;

//             this.scroller.viewport = dataGroup;
//             this.scroller.width = dataGroup.width;
//             this.scroller.height = dataGroup.height;
//             this.scroller.x = dataGroup.x;
//             this.scroller.y = dataGroup.y;
//             this.scroller.scrollPolicyV = eui.ScrollPolicy.ON;
//             this.scroller.scrollPolicyH = eui.ScrollPolicy.OFF;
//             if (!manager.GameManager.getInstance().adManager.isChecking()) {
//                 // this.loadApps();
//             }
//         }


//         private onOpenHold(eve: egret.Event): void {
//             egret.log("打开抽屉");
//             let that = this;
//             that.bgImg.visible = true;
//             egret.Tween.get(this.adGroup).to({x: 0}, 400).call(function () {
//                 that.buttonHoldOpen.visible = false;
//                 that.buttonHoldClose.visible = true;
//             });
//         }

//         private onCloseOpen(eve: egret.Event): void {
//             let that = this;
//             that.bgImg.visible = false;
//             egret.Tween.get(this.adGroup).to({x: -this.holdBg.width},
//                 400).call(function () {
//                 that.buttonHoldOpen.visible = true;
//                 that.buttonHoldClose.visible = false;
//             });
//         }

//         private loadApps(): void {
//             if (!manager.GameManager.isProduct) {
//                 return;
//             }
//             // let request: egret.HttpRequest = new egret.HttpRequest();
//             // request.responseType = egret.HttpResponseType.TEXT;
//             // let url = GameAdPage.APP_ID_URL + manager.GameManager.getInstance().getAppToken();
//             // request.open(url, egret.HttpMethod.GET);
//             // request.send();
//             // request.addEventListener(egret.Event.COMPLETE, this.onLoadComplete, this);
//             this.onLoadComplete()
//         }

//         private onLoadComplete(): void {
//             // let request = <egret.HttpRequest>event.currentTarget;
//             // let data = JSON.parse(request.response);
//             // data.data.shift()
//             // egret.log("app data:", data);            
//             // GameAdPage.adImgDatas = data.data;
//             // this.appIdDataGroup.dataProvider = new eui.ArrayCollection(GameAdPage.adImgDatas);


//             console.log(manager.GameManager.getInstance().getAdManager().adPage)
//             console.log(Main.getInstance().gameManager.getAdManager().adPage)
//             let data = Main.getInstance().gameManager.getAdManager().adPage.adList
//             GameAdPage.adImgDatas = data
//             this.appIdDataGroup.dataProvider = new eui.ArrayCollection(GameAdPage.adImgDatas);
//         }
//     }


//     export class AdImgData {
//         /*
//         "id": "16",
//         "on": "1",
//         "token": "",
//         "appid": "wxfc88730de46e6d73",
//         "path": "view\/pages\/index?wxa_tunnel=1573&appid=wx42d12a5790960727&path=pages/index/index?ald_media_id=2942&ald_link_key=b79f73724c1b6f03&ald_position_id=0&wxa_tunnel=1573",
//         "type": "0",
//         "img": "http:\/\/wx.11babay.cn\/uploads\/d\/djnwit1520825993\/5\/4\/b\/3\/5b9789d4dd0a6.gif",
//         "session": "0",
//         "title": "龙卷风乱斗",
//         "icon": "http:\/\/wx.11babay.cn\/uploads\/d\/djnwit1520825993\/5\/4\/b\/3\/5b9789d4dd0a6.gif",
//         "weight": "99"
//         */

//         public appid: string;
//         public title: string;
//         public path: string;
//         public icon: string;
//         public img: string;

//         public extraData: any = {};
//     }

//     export class AdItemRender extends eui.ItemRenderer {

//         private txtTitle: eui.Label;

//         private iconImg: eui.Image;

//         public constructor() {
//             super();
//         }

//         public dataChanged() {
//             if (this.data) {
//                 let adData: AdImgData = this.data;
//                 this.txtTitle.text = adData.title;
//                 this.txtTitle.width = 110;                
//                 this.txtTitle.height = 20;                
//                 this.loadIconImg();
//             }
//         }

//         private loadIconImg(): void {

//             let adData: AdImgData = this.data;
//             let that = this;
//             let imgLoader = new egret.ImageLoader();
//             imgLoader.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
//                 let imageLoader = <egret.ImageLoader>event.currentTarget;
//                 that.updateTexture(imageLoader.data);
//             }, this);
//             imgLoader.load(adData.img);
//             this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTaped, this);
//         }

//         private updateTexture(bitmapData: egret.BitmapData): void {
//             let texture = new egret.Texture();
//             texture.bitmapData = bitmapData;
//             this.iconImg.texture = texture;
//             this.iconImg.width = this.iconImg.height = 110;
//             this.txtTitle.size = 20;
//             this.txtTitle.textColor = 0xffffff;
//             this.txtTitle.width = 110;
//             this.txtTitle.height = 20;
//             this.txtTitle.y = this.iconImg.height + 10;
//         }

//         private onTaped(eve: egret.Event): void {
//             egret.log("点击跳转其他小程序");
//             eve.stopImmediatePropagation();
//             wx.navigateToMiniProgram({
//                 appId: this.data.appid,
//                 path: this.data.path,
//                 extraData: this.data.extraData || {}
//             });
//         }
//     }

// }








module game {

    export class GameAdPage extends eui.Component {

        public static readonly SHOW_FRIEND_RANK: string = "show_friend_rank";

        public static readonly PLAY_GAME_EVENT: string = "start_game";

        public holdBg: eui.Image;

        public static adImgDatas: Array<AdImgData>;

        public rootGroup: eui.Group;

        public scroller: eui.Scroller;

        public appIdDataGroup: eui.DataGroup;

        private btnHoldOpen: eui.Image;

        private btnHoldClose: eui.Image;

        public btnGetRewardImg: eui.Image;

        public adDataGroup: eui.Group;

        public adGroup: eui.Group;

        public bgImg: eui.Image;

        private buttonHoldOpen: ui.PushButton;

        private buttonHoldClose: ui.PushButton;

        private buttonGetReward: ui.PushButton;

        private static readonly APP_ID_URL: string = "https://2zhuji.cn/index.php?g=WxaGame&m=Ad&a=get_ad_list&token=";

        constructor() {
            super();
            this.addEventListener(eui.UIEvent.COMPLETE, this.onComplete, this);
            this.skinName = "resource/assets/GameRes/game/skins/GameActivityPageSkin.exml";
        }

        public show(): void {
            this.visible = true;
        }

        protected createChildren() {
            super.createChildren();
        }

        public resize(): void {

        }

        private onComplete(): void {
            this.bgImg.visible = false;
            this.bgImg.width = manager.DisplayManager.STAGE_WIDTH;
            this.bgImg.height = manager.DisplayManager.STAGE_HEIGHT;
            this.buttonHoldClose = new ui.PushButton(this.btnHoldClose);
            this.buttonHoldClose.addEventListener(ui.PushButton.CLICK, this.onCloseOpen, this);
            this.adDataGroup.addChild(this.buttonHoldClose);

            this.buttonHoldOpen = new ui.PushButton(this.btnHoldOpen);
            this.buttonHoldOpen.addEventListener(ui.PushButton.CLICK, this.onOpenHold, this);
            this.adDataGroup.addChild(this.buttonHoldOpen);

            this.buttonHoldClose.visible = false;
            this.buttonHoldOpen.visible = true;

            // this.buttonGetReward = new ui.PushButton(this.btnGetRewardImg);
            // this.buttonGetReward.addEventListener(ui.PushButton.CLICK, this.onGetReward, this);
            // this.addChild(this.buttonGetReward);

            let dataGroup = this.appIdDataGroup;//对象是eui.DataGroup;
            dataGroup.itemRenderer = AdItemRender;
            dataGroup.useVirtualLayout = true;
            dataGroup.scrollEnabled = true;

            let tLayout: eui.TileLayout = new eui.TileLayout();

            tLayout.requestedRowCount = 2;
            tLayout.requestedColumnCount = 3;
            tLayout.verticalGap = 5;
            tLayout.horizontalGap = 5;
            dataGroup.layout = tLayout;

            this.scroller.viewport = dataGroup;
            this.scroller.width = dataGroup.width;
            this.scroller.height = dataGroup.height;
            this.scroller.x = dataGroup.x;
            this.scroller.y = dataGroup.y;
            this.scroller.scrollPolicyV = eui.ScrollPolicy.ON;
            this.scroller.scrollPolicyH = eui.ScrollPolicy.OFF;
            if (!manager.GameManager.getInstance().adManager.isChecking()) {
                this.loadApps();
            }
        }

        // private onGetReward(eve: egret.Event): void {
        //     let tixianPage: TiXIanPage = new TiXIanPage();
        //     this.addChild(tixianPage);
        //     tixianPage.show();
        //     manager.StatisticsManager.click(31);
        // }

        private onOpenHold(eve: egret.Event): void {
            egret.log("打开抽屉");
            let that = this;
            that.bgImg.visible = true;
            egret.Tween.get(this.adGroup).to({x: 0}, 400).call(function () {
                that.buttonHoldOpen.visible = false;
                that.buttonHoldClose.visible = true;
            });
            manager.StatisticsManager.click(30);
        }

        private onCloseOpen(eve: egret.Event): void {
            let that = this;
            that.bgImg.visible = false;
            egret.Tween.get(this.adGroup).to({x: -this.holdBg.width},
                400).call(function () {
                that.buttonHoldOpen.visible = true;
                that.buttonHoldClose.visible = false;
            });
        }

        private loadApps(): void {
            if (!manager.GameManager.isProduct) {
                return;
            }
            this.appIdDataGroup.dataProvider = new eui.ArrayCollection(Main.getInstance().gameManager.getAdManager().drawer_ad);
            this.dispatchEvent(new egret.Event(GameAdPage.LOAD_AD_CONFIG_COMPLETE));
        }

        public static LOAD_AD_CONFIG_COMPLETE:string = "load_ad_config_complete";

    }


    export class AdImgData {
        /*
        "id": "16",
        "on": "1",
        "token": "",
        "appid": "wxfc88730de46e6d73",
        "path": "view\/pages\/index?wxa_tunnel=1573&appid=wx42d12a5790960727&path=pages/index/index?ald_media_id=2942&ald_link_key=b79f73724c1b6f03&ald_position_id=0&wxa_tunnel=1573",
        "type": "0",
        "img": "http:\/\/wx.11babay.cn\/uploads\/d\/djnwit1520825993\/5\/4\/b\/3\/5b9789d4dd0a6.gif",
        "session": "0",
        "title": "龙卷风乱斗",
        "icon": "http:\/\/wx.11babay.cn\/uploads\/d\/djnwit1520825993\/5\/4\/b\/3\/5b9789d4dd0a6.gif",
        "weight": "99"
        */

        public appid: string;
        public title: string;
        public path: string;
        public icon: string;
        public img: string;

        public extraData: any = {};
    }

    export class AdItemRender extends eui.ItemRenderer {

        private txtTitle: eui.Label;

        private iconImg: eui.Image;

        public constructor() {
            super();
        }

        public dataChanged() {
            if (this.data) {
                let adData: AdImgData = this.data;
                this.txtTitle.text = adData.title.substring(0, 5);
                this.loadIconImg();
            }
        }

        private loadIconImg(): void {
            let adData: AdImgData = this.data;
            let that = this;
            let imgLoader = new egret.ImageLoader();
            imgLoader.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                let imageLoader = <egret.ImageLoader>event.currentTarget;
                that.updateTexture(imageLoader.data);
            }, this);
            imgLoader.load(adData.img);
            this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTaped, this);
        }

        private updateTexture(bitmapData: egret.BitmapData): void {
            let texture = new egret.Texture();
            texture.bitmapData = bitmapData;
            this.iconImg.texture = texture;
            this.iconImg.width = this.iconImg.height = 110;
            this.txtTitle.size = 20;
            this.txtTitle.textColor = 0x0;
            this.txtTitle.width = 110;
            this.txtTitle.y = this.iconImg.height + 10;
        }

        private onTaped(eve: egret.Event): void {
            egret.log("点击跳转其他小程序");
            eve.stopImmediatePropagation();
            manager.StatisticsManager.click(42);
            manager.GameManager.getInstance().getAdManager().showAd("drawer_ad", this.data);
        }
    }

}